package org.lord.slark.numbers.activity;

import android.app.Activity;
import android.content.DialogInterface;
import android.content.Intent;
import android.database.sqlite.SQLiteDatabase;
import android.net.Uri;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import org.lord.slark.numbers.R;
import org.lord.slark.numbers.db.LocalDB;
import org.lord.slark.numbers.entity.Saint;
import org.lord.slark.numbers.utils.LocalConstants;

public class SaintDetail extends AppCompatActivity implements View.OnClickListener {
    private ImageView imgback;
    private TextView tvnamevalue, tvphonevalue, tvdelete, tvedit, tvleft, tvcall;
    private Saint mSaint;
    private int mSaintId;
    private static final int REQUEST_CODE = 106;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        getSupportActionBar().hide();
        setContentView(R.layout.activity_saint_detail);
        initView();
        dealWithIntent();
    }

    private void initView() {
        tvnamevalue = (TextView) findViewById(R.id.tvnamevalue);
        tvphonevalue = (TextView) findViewById(R.id.tvphonevalue);
        imgback = (ImageView) findViewById(R.id.imgback);
        tvleft = (TextView) findViewById(R.id.tvleft);
        tvedit =  (TextView) findViewById(R.id.tvedit);
        tvdelete = (TextView) findViewById(R.id.tvdelete);
        tvcall = (TextView) findViewById(R.id.tvcall);
        imgback.setOnClickListener(this);
        tvleft.setOnClickListener(this);
        tvdelete.setOnClickListener(this);
        tvedit.setOnClickListener(this);
        tvcall.setOnClickListener(this);
    }

    private void dealWithIntent() {
        mSaint = (Saint) getIntent().getSerializableExtra(LocalConstants.SAINT);
        if (mSaint != null) {
            mSaintId = mSaint.getId();
            tvnamevalue.setText(mSaint.getName());
            tvphonevalue.setText(mSaint.getPhone());
        }
    }

    /**
     * Called when a view has been clicked.
     *
     * @param v The view that was clicked.
     */
    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.imgback:
            case R.id.tvleft:
                onBackPressed();
                break;
            case R.id.tvdelete:
                new AlertDialog.Builder(this).setMessage(getString(R.string.deletetoast))
                        .setPositiveButton(getString(R.string.ok), new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialog, int which) {
                                deleteSaintFromDB(mSaint);
                                SaintDetail.this.finish();
                            }
                        }).setNegativeButton(getString(R.string.cancel), null).create().show();
                break;
            case R.id.tvedit:
                Intent i = new Intent(this, AddSaintActivity.class);
                i.putExtra(LocalConstants.SAINT, mSaint);
                startActivityForResult(i, REQUEST_CODE);
                break;
            case R.id.tvcall:
                Intent icall = new Intent();
                icall.setAction(Intent.ACTION_CALL);
                icall.setData(Uri.parse("tel:"+mSaint.getPhone()));
                startActivity(icall);
                break;
            default:
                break;
        }
    }

    private void deleteSaintFromDB(Saint saint) {
        SQLiteDatabase db = LocalDB.getInstance(this).getWritableDatabase();
        try {
            db.delete(Saint.TABLE_NAME, Saint.COLUMN_ID + " = ?", new String[]{String.valueOf(saint.getId())});
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            db.close();
        }
    }

    /**
     * Dispatch incoming result to the correct fragment.
     *
     * @param requestCode
     * @param resultCode
     * @param data
     */
    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        if (requestCode == REQUEST_CODE && resultCode == RESULT_OK) {
            Saint s = (Saint)data.getSerializableExtra(LocalConstants.SAINT);
            tvnamevalue.setText(s.getName());
            tvphonevalue.setText(s.getPhone());
            mSaint.setName(s.getName());
            mSaint.setPhone(s.getPhone());
        }
    }
}
